package org.example;

import org.example.bean.User;
import org.example.web.UserAction;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/4/15
 * Email haijunzhou@hnit.edu.cn
 */
@Configuration   // beans.xml
// 组件扫描
@ComponentScan("org.example")
@EnableAspectJAutoProxy
// 开始事务管理
@EnableTransactionManagement
public class BeanConfig {

    @Bean("user0")  // <bean>
    @Primary
    public User user0Ohter(){
        return new User(){
            //  匿名类
            {
                // 实例代码块 => 无名构造函数
            this.setName("张三");
        }};
    }

    @Bean  // <bean>
    public User user1(){
        return new User(){
            //  匿名类
            {
                // 实例代码块 => 无名构造函数
                this.setName("张三丰");
            }};
    }



    @Bean(destroyMethod = "destory")  // <bean>
    @Primary
    @Lazy
    public User user3( User user0 ){
        return user0.create();
    }

    @Bean
    @Primary
    public UserAction userAction1(){
        return new UserAction(){{
            this.setId(100);
        }};
    }


    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///bank");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    @Bean("transactionManager")
    // 事务管理器
    public DataSourceTransactionManager dataSourceTransactionManager(
            DataSource dataSource
    ){
        return new DataSourceTransactionManager(dataSource);
    }



}
